//package com.digiwin.dap.middleware.trace;
//
//import org.springframework.boot.actuate.trace.http.HttpExchangeTracer;
//import org.springframework.boot.actuate.trace.http.HttpTrace;
//import org.springframework.boot.actuate.trace.http.HttpTraceRepository;
//import org.springframework.boot.actuate.web.trace.servlet.HttpTraceFilter;
//import org.springframework.core.Ordered;
//import org.springframework.http.HttpStatus;
//
//import javax.servlet.FilterChain;
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpServletResponseWrapper;
//import javax.servlet.http.HttpSession;
//import java.io.IOException;
//import java.net.URI;
//import java.net.URISyntaxException;
//
///**
// * @author chenxsa
// */
//public class HttpTraceWithBodyFilter extends HttpTraceFilter implements Ordered {
//    public HttpTraceWithBodyFilter(HttpTraceRepository repository, HttpExchangeTracer tracer) {
//        super(repository,tracer);
//        this.repository = repository;
//        this.tracer = tracer;
//    }
//    private final HttpTraceRepository repository;
//    private final HttpExchangeTracer tracer;
//    @Override
//    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
//        if (!this.isRequestValid(request)) {
//            filterChain.doFilter(request, response);
//        } else {
//            TraceableHttpServletRequest traceableRequest = new TraceableHttpServletRequest(request);
//            HttpTrace trace = this.tracer.receivedRequest(traceableRequest);
//            int status = HttpStatus.INTERNAL_SERVER_ERROR.value();
//            boolean var11 = false;
//
//            try {
//                var11 = true;
//                filterChain.doFilter(request, response);
//                status = response.getStatus();
//                var11 = false;
//            } finally {
//                if (var11) {
//                    TraceableHttpServletResponse traceableResponse = new TraceableHttpServletResponse((HttpServletResponse)(status != response.getStatus() ? new HttpTraceFilter.CustomStatusResponseWrapper(response, status) : response));
//                    this.tracer.sendingResponse(trace, traceableResponse, request::getUserPrincipal, () -> {
//                        return this.getSessionId(request);
//                    });
//                    this.repository.add(trace);
//                }
//            }
//
//            TraceableHttpServletResponse traceableResponse = new TraceableHttpServletResponse((HttpServletResponse)(status != response.getStatus() ? new HttpTraceFilter.CustomStatusResponseWrapper(response, status) : response));
//            this.tracer.sendingResponse(trace, traceableResponse, request::getUserPrincipal, () -> {
//                return this.getSessionId(request);
//            });
//            this.repository.add(trace);
//        }
//    }
//
//    private boolean isRequestValid(HttpServletRequest request) {
//        try {
//            new URI(request.getRequestURL().toString());
//            return true;
//        } catch (URISyntaxException var3) {
//            return false;
//        }
//    }
//
//    private String getSessionId(HttpServletRequest request) {
//        HttpSession session = request.getSession(false);
//        return session != null ? session.getId() : null;
//    }
//
//    private static final class CustomStatusResponseWrapper extends HttpServletResponseWrapper {
//        private final int status;
//
//        private CustomStatusResponseWrapper(HttpServletResponse response, int status) {
//            super(response);
//            this.status = status;
//        }
//
//        @Override
//        public int getStatus() {
//            return this.status;
//        }
//    }
//}
